package com.skt.common_hr.work.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.approval.IRequestApprovalProxy;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	//import com.skt.common_hr.training.model.vo.TrainingDetailVO;
	import com.skt.common_hr.work.model.vo.ReqVacationVO;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;

	public class VacationApprovalProxy extends CommonProxy implements IRequestApprovalProxy
	{
		public static const NAME:String = "VacationApprovalProxy";
		public var dataProvider:Array;
		
		private var templateHTML:String = "";
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function VacationApprovalProxy(data:Object=null)
		{
			super(NAME, data);
			loadTemplate();
		}
		
		private var _key:String = "";
		public function get key():String
		{
			trace("결재 키: " + _key);
			return _key;
		}
		public function set key(k:String):void
		{
			_key = k;
		}
		
		public function get title():String 
		{
			var tmp:String = "휴가 품의";
			/*
			if(DateUtil.daySpan(dataProvider.BEGDA, dataProvider.ENDDA) > 1 ) {
				tmp += DateUtil.toDefaultString(dataProvider.BEGDA) + " ~ " + DateUtil.toDefaultString(dataProvider.ENDDA) + ")";
			}
			else {
				tmp += DateUtil.toDefaultString(dataProvider.BEGDA) + ")";
			}
			*/
			return tmp;
		}
		
		public function get content():String
		{
			return buildHTML(dataProvider);
		}
		
		public function get appCode():String 
		{
			return "01"; 	//
		}
 		
 		private function loadTemplate():void
		{
			var request:URLRequest = new URLRequest("assets/template/vacation.tmp");
			loader.addEventListener(Event.COMPLETE, templateResult);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
		}
 			
		private function templateResult(event:Event):void
		{
			templateHTML = loader.data;
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void
		{
			trace("ioErrorHandler: " + event);
		}



		private function buildHTML(table:Array):String
		{
			var htmlAll:String = templateHTML;
			var htmlBlock:String = "";

			for each(var vo:ReqVacationVO in table)
			{
				var htmlLine:String = "<TR>"
				htmlLine +=  "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid; border-right-width : 1px;' width=80 height=20>&nbsp;" + StringUtil.NVL(vo.AWATX) + "</TD>";
				htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid; border-right-width : 1px;' width=90 height=20>&nbsp;" + DateUtil.toDefaultString(vo.BEGDA) + "</TD>";
				htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid; border-right-width : 1px;' width=90 height=20>&nbsp;" + DateUtil.toDefaultString(vo.ENDDA) + "</TD>";
				if(vo.APM!=null) {
					htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid; border-right-width : 1px;' width=70 height=20>&nbsp;" + ((vo.APM=="A") ? "오전" : "오후") + "</TD>";
				}
				else {
					htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid; border-right-width : 1px;' width=70 height=20>&nbsp;</TD>";
				}
				htmlLine += "<TD style='background: #fdfdf3; text-align: left; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;  border-right-width : 1px;' width=270 height=20>&nbsp;" + StringUtil.NVL(vo.TEXT1) + "</TD></TR>";
				htmlBlock += htmlLine;
			}
			
			var ptnBlock:RegExp = /__RECORD_BLOCK__/; 
			var ptnMember:RegExp = /__MEMBER__/;
			htmlAll = htmlAll.replace(ptnMember, Variables.myName+"("+Variables.myPernr+")");
			htmlAll = htmlAll.replace(ptnBlock, htmlBlock);
			return htmlAll;
		}
		

	}
}